package com.faithbj.support.updownload;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.faithbj.utils.SystemConfigUtil;


@Component("FileUploadHandler")
public class FileUploadHandlerSaveToLocalDisk implements FileUploadHandler {

    protected final Logger logger = LoggerFactory.getLogger(getClass());
    
    public static final Integer UPLOAD_PERMITION=0;
    public static final Integer UPLOAD_NOT_ALLOWED=1;
    public static final Integer EXTENSIONS_NOT_ALLOWED=2;
    public static final Integer SIZE_NOT_ALLOWED=3;
    

    
    @Resource
    private SystemConfigUtil systemConfigUtil;
    
    private Set<String> allowedFileExtensionSet = new HashSet<String>();

    @PostConstruct
    public void init() {
        if (StringUtils.hasText(systemConfigUtil.getAllowedUploadFileExtension())) {
            allowedFileExtensionSet.addAll(Arrays.asList(systemConfigUtil.getAllowedUploadFileExtension().split(",")));
        }
    }

	@Override
	public Boolean uploadByFilePath(MultipartFile file, String filename) {
		File localFile=new File(filename);
		if(!localFile.getParentFile().isDirectory()){
			synchronized(FileUploadHandlerSaveToLocalDisk.class){
				localFile.mkdirs();
			}
		}
	    try 
	    {
	        file.transferTo(localFile);
	    } 
	    catch (IOException ioe) 
	    {
	        logger.error("Fail to upload file: " + file.getOriginalFilename(), ioe);
	        return false;
	    }
	        logger.info("upload Success!");
	        return true;
	}

	
	protected void checkFileExtention(String fileName) {
		boolean allowed = false;
	    if (StringUtils.hasLength(fileName) && fileName.contains(".")) {
	    	String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
	        if (allowedFileExtensionSet.contains(extension.trim().toLowerCase())) {
	            allowed = true;
	        }
	     }
	     if (!allowed) {
	        throw new IllegalArgumentException("File extension not allowed for: " + fileName);
	     }
	}
	@Override
	public Map<Integer, String> checkFileExtention(MultipartFile logo) {
		Map<Integer, String> map=new HashMap<Integer, String>();
	
		if (!StringUtils.hasLength(systemConfigUtil.getAllowedUploadFileExtension())){
			map.put(UPLOAD_NOT_ALLOWED, "不允许上传图片文件!");
			return map;
		}
		String fileName=logo.getOriginalFilename();
		if (!fileName.contains(".")) {
			map.put(EXTENSIONS_NOT_ALLOWED, "只允许上传图片文件类型:"+systemConfigUtil.getAllowedUploadFileExtension());
			return map;
		}
		String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
		if (!allowedFileExtensionSet.contains(extension.trim().toLowerCase())) {
			map.put(EXTENSIONS_NOT_ALLOWED, "只允许上传图片文件类型:"+systemConfigUtil.getAllowedUploadFileExtension());
			return map;
        }
		if (systemConfigUtil.getUploadLimit() != 0 && logo.getSize() > systemConfigUtil.getUploadLimit()) {
			map.put(SIZE_NOT_ALLOWED, "Logo文件大小超出限制!");
			return map;
		}
		map.put(UPLOAD_PERMITION, "可以上传文件!");
		return map;
	}


	@Override
	public String handleUpload(MultipartFile file, String fileType,
			int isScaleImage) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void generateScaleImage(String sourceImagePath, String bigImagePath,
			int scaleType) {
		// TODO Auto-generated method stub
		
	}

}
